package com.youth.executor;

import com.youth.entity.po.Commodity;
import com.youth.mapper.ICommodityMaper;
import com.youth.utils.ExcelReaderUtil;
import com.youth.utils.MySpringUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * @ClassName CommodityThread
 * @Description: TODO
 * @Author yjwu
 * @Date 2019/12/7 19:45
 **/
@Slf4j
public class AddByExcelThread implements Callable {
    private List<Commodity> list;
    private CountDownLatch countDownLatch;

    public AddByExcelThread(List<Commodity> list, CountDownLatch countDownLatch) {
        this.list = list;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public Integer call() throws Exception {
        ICommodityMaper commodityMaper = MySpringUtil.getBean(ICommodityMaper.class);
        Integer result = 0;
        if (list.size() > 1000) {
            List<List<Commodity>> lists = ExcelReaderUtil.splitList(list, 1000);
            for (int i = 0; i < lists.size(); i++) {
                result += commodityMaper.addByList(lists.get(i));
            }
        } else {
            result += commodityMaper.addByList(list);
        }
        countDownLatch.countDown();
        return result;
    }
}
